<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TravelAdminController
 *
 * @author dro
 */

namespace Gairal\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Gairal\CoreBundle\Form\TravelType;
use Gairal\CoreBundle\Entity\Travel;

class TravelAdminController extends Controller {

    public function indexAction() {

        $listTravels = $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Travel')
                ->findBy(array(), array('displayOrder' => 'asc'));
        $maxOrder = count($listTravels) > 0 ? $listTravels[count($listTravels) - 1]->getDisplayOrder() : 0;

        return $this->render('GairalAdminBundle:Travel:index.html.twig', array(
                    'action' => 'Admin - Travels',
                    'listTravels' => $listTravels,
                    'maxOrder' => $maxOrder
        ));
    }

    public function addAction() {

        $travel = new Travel();
        $form = $this->createForm(new TravelType(), $travel);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {
                $maxOrder = $this->getDoctrine()
                        ->getManager()
                        ->getRepository('GairalCoreBundle:Travel')
                        ->findMaxOrder();
                $travel->setDisplayOrder(++$maxOrder);
                $em = $this->getDoctrine()->getManager();
                $em->persist($travel);
                $em->flush();

                $this->get('session')->getFlashBag()->add('info', 'Place "' . $travel->getPlace() . '" added');
                return $this->redirect($this->generateUrl('AdminTravel'));
            }
            else
                $this->get('session')->getFlashBag()->add('error', $form->getErrorsAsString());
        }
        
        return $this->render('GairalAdminBundle:Travel:add.html.twig', array(
                    'action' => 'Admin - Travel - Add',
                    'form' => $form->createView()
        ));
    }

    public function removeAction(Travel $travel) {

        if ($travel != null) {
            $this->getDoctrine()
                    ->getManager()
                    ->getRepository('GairalCoreBundle:Travel')
                    ->removeAndReorder($travel);
            $this->get('session')->getFlashBag()->add('info', 'Travel "' . $travel->getPlace() . '" removed');
        }
        else
            $this->get('session')->getFlashBag()->add('error', 'Travel not removed, error in url');

        return $this->redirect($this->generateUrl('AdminTravel'));
    }

    public function modifyAction(Travel $travel) {
        $form = $this->createForm(new TravelType(), $travel);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {

                $em = $this->getDoctrine()->getManager();
                $em->persist($travel);
                $em->flush();

                $this->get('session')->getFlashBag()->add('info', 'Travel "' . $travel->getPlace() . '" modified');
                return $this->redirect($this->generateUrl('AdminTravel'));
            }
            else
                $this->get('session')->getFlashBag()->add('error', $form->getErrorsAsString());
        }
        return $this->render('GairalAdminBundle:Travel:modify.html.twig', array(
                    'action' => 'Admin - Travel - modify - ' . $travel->getPlace(),
                    'form' => $form->createView(),
                    'travel' => $travel
        ));
    }

    public function upAction(Travel $travel) {
        $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Travel')
                ->swapWith($travel, $travel->getDisplayOrder() + 1);
        $this->get('session')->getFlashBag()->add('info', 'Travel "' . $travel->getPlace() . '" down\'d');

        return $this->redirect($this->generateUrl('AdminTravel'));
    }

    public function downAction(Travel $travel) {
        $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Travel')
                ->swapWith($travel, $travel->getDisplayOrder() - 1);
        $this->get('session')->getFlashBag()->add('info', 'Travel "' . $travel->getPlace() . '" up\'d');

        return $this->redirect($this->generateUrl('AdminTravel'));
    }

}

?>
